package bit.chapter;

/**
 * chapterNo 用于记录章节所在图书的位置，只是用于计算章节在二进制中的位置而已。
 * chapterId 用于记录图书的唯一标识
 */
public class ChapterNo
{
    private final int value;

    private final int start;

    private final int offset;

    private String chapterId;

    public ChapterNo(int chapterNo)
    {
        this.value = chapterNo;
        this.start = chapterNo / Constant.ONE_RECORD_CHAPTER_NUM * Constant.ONE_RECORD_CHAPTER_NUM;
        this.offset = chapterNo % Constant.ONE_RECORD_CHAPTER_NUM;
    }

    public ChapterNo(int start, int offset)
    {
        this.value = start + offset;
        this.start = start;
        this.offset = offset;
    }

    public ChapterNo(int chapterNo, String chapterId)
    {
        this(chapterNo);
        this.chapterId = chapterId;
    }

    public int getValue()
    {
        return value;
    }

    public int getStart()
    {
        return start;
    }

    public int getOffset()
    {
        return offset;
    }

    /**
     * @return the chapterId
     */
    public String getChapterId()
    {
        return chapterId;
    }

    /**
     * @param chapterId the chapterId to set
     */
    public void setChapterId(String chapterId)
    {
        this.chapterId = chapterId;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + value;
        return result;
    }

    // begin no checkstyle
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (!(obj instanceof ChapterNo))
        {
            return false;
        }
        ChapterNo other = (ChapterNo) obj;
        if (value != other.value)
        {
            return false;
        }
        return true;
    }

    // end no checkstyle

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append("ChapterNo [value=");
        builder.append(value);
        builder.append(", start=");
        builder.append(start);
        builder.append(", offset=");
        builder.append(offset);
        builder.append(", chapterId=");
        builder.append(chapterId);
        builder.append("]");
        return builder.toString();
    }

}